/**
 * getClassList   获取班级列表
 * queryClassList 根据name模糊查询班级
 * addClass       添加班级
 * getInfoClass   获取班级信息
 * updateClass    修改班级信息
 * deleteClass    删除班级
 */
const classStatement = require("../../common/sql_sentences/class")
const facultyStatement = require("../../common/sql_sentences/faculty")
const gradeStatement = require("../../common/sql_sentences/grade")

exports.getClassList = async (req, res) => {
  try {
    if (req.query.name) {
      let { faculty, grade, page, pagesize, name } = req.query
      // 根据name模糊查询班级总数
      let total,
        sqlStr = classStatement.selectQueryCountLike(name, { faculty, grade })
      let results = await RunSQL(sqlStr)
      total = results[0].total

      // 根据name模糊查询获取班级列表
      sqlStr = classStatement.selectQueryLikeLimit(name, page, pagesize, { faculty, grade })
      results = await RunSQL(sqlStr)
      return res.successSend("", 10000, { row: results, total })
    } else {
      let { faculty, grade, page, pagesize } = req.query
      // 获取班级总数
      let total,
        sqlStr = classStatement.selectCount({ faculty, grade })
      let results = await RunSQL(sqlStr)
      if (!results[0].total) return res.successSend("没有找到班级信息！", 10005)
      total = results[0].total

      if (!pagesize) {
        pagesize = total
      }

      // 获取班级列表
      sqlStr = classStatement.selectLimit({ faculty, grade }, page, pagesize)

      results = await RunSQL(sqlStr)

      return res.successSend("", 10000, { row: results, total })
    }
  } catch (error) {
    res.errSend(error)
  }

}

exports.addClass = async (req, res) => {
  try {
    const data = req.body
    // 查询班级名称是否重复
    let sqlStr = classStatement.select({ name: data.name })
    let results = await RunSQL(sqlStr)
    if (results.length) return res.successSend("名称已存在！", 10005)

    // 查询学部是否存在
    sqlStr = facultyStatement.selectCount({ name: data.faculty })
    results = await RunSQL(sqlStr)
    if (!results[0].total) return res.successSend("学部不存在！", 10005)


    // 查询年级是否存在
    sqlStr = gradeStatement.selectCount({ name: data.grade })
    results = await RunSQL(sqlStr)
    if (!results[0].total) return res.successSend("年级不存在！", 10005)

    // 添加班级
    sqlStr = classStatement.insert(req.body)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("添加班级失败！", 10005)
    return res.successSend("添加班级成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.getInfoClass = async (req, res) => {
  try {
    // 获取班级信息
    let sqlStr = classStatement.idSelect(req.body.id)
    let results = await RunSQL(sqlStr)
    if (results.length !== 1)
      return res.successSend("获取班级信息失败！", 10005)
    return res.successSend("", 10000, results[0])
  } catch (error) {
    res.errSend(error)
  }
}

exports.updateClass = async (req, res) => {
  try {
    const data = req.body

    // 查询班级名称是否重复
    let sqlStr = classStatement.select({ name: data.name })
    let results = await RunSQL(sqlStr)
    if (results[0]?.id && results[0]?.id !== data.id) return res.successSend("名称已存在！", 10005)

    // 查询学部是否存在
    sqlStr = facultyStatement.select({ name: data.faculty })
    results = await RunSQL(sqlStr)
    if (!results.length) return res.successSend("学部不存在！", 10005)


    // 查询年级是否存在
    sqlStr = gradeStatement.select({ name: data.grade })
    results = await RunSQL(sqlStr)
    if (!results.length) return res.successSend("年级不存在！", 10005)

    // 修改班级信息
    sqlStr = classStatement.idUpdate(data, data.id)
    results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("修改班级信息失败！", 10005)
    return res.successSend("修改班级信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}

exports.deleteClass = async (req, res) => {
  try {
    // 删除班级
    let sqlStr = classStatement.delete(req.params.id)
    let results = await RunSQL(sqlStr)
    if (results.affectedRows !== 1)
      return res.successSend("删除班级失败！", 10005)
    return res.successSend("删除班级信息成功！")
  } catch (error) {
    res.errSend(error)
  }
}
